{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 基金日数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                 date   open  close   high    low    volume\n",
      "date                                                       \n",
      "2005-02-23 2005-02-23  0.881  0.876  0.882  0.866  12697425\n",
      "2005-02-24 2005-02-24  0.876  0.876  0.876  0.868   4516142\n",
      "2005-02-25 2005-02-25  0.877  0.880  0.887  0.875   5064607\n",
      "2005-02-28 2005-02-28  0.878  0.872  0.879  0.870   1879652\n",
      "2005-03-01 2005-03-01  0.870  0.867  0.873  0.865   2080945\n",
      "...               ...    ...    ...    ...    ...       ...\n",
      "2024-06-25 2024-06-25  3.108  3.099  3.114  3.086  10331747\n",
      "2024-06-26 2024-06-26  3.094  3.103  3.110  3.088   8626790\n",
      "2024-06-27 2024-06-27  3.098  3.096  3.101  3.083   9311183\n",
      "2024-06-28 2024-06-28  3.090  3.103  3.115  3.088   7429559\n",
      "2024-07-01 2024-07-01  3.102  3.105  3.110  3.094   3946129\n",
      "\n",
      "[4705 rows x 6 columns]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\baoww\\AppData\\Local\\Temp\\ipykernel_11636\\986110979.py:22: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  etf_df['date'] = pd.to_datetime(etf_df['date'])\n"
     ]
    }
   ],
   "source": [
    "import akshare as ak\n",
    "import pandas as pd\n",
    "\n",
    "class etf_daily:\n",
    "    \"\"\"\n",
    "    获取股票数据，复权设置\n",
    "    # 示例使用\n",
    "        symbol = \"510050\"\n",
    "        adjust = \"hfq\"\n",
    "\n",
    "        etf = etf_daily(symbol,  adjust)\n",
    "        data = etf.get_data()\n",
    "        print(data)\n",
    "    \n",
    "    \"\"\"\n",
    "    def __init__(self, symbol, adjust):\n",
    "        self.symbol = symbol\n",
    "        self.adjust = adjust\n",
    "        self.data = None\n",
    "        \n",
    "    def get_data(self):\n",
    "        '''获取股票后复权数据'''\n",
    "        fund_etf_hist_em_df = ak.fund_etf_hist_em(symbol=self.symbol, \n",
    "                                                  period=\"daily\", \n",
    "                                                  adjust=self.adjust)\n",
    "        selected_columns = ['日期', '开盘', '收盘', '最高', '最低', '成交量']\n",
    "        etf_df = fund_etf_hist_em_df[selected_columns]\n",
    "        \n",
    "        # 处理字段命名\n",
    "        etf_df.columns = ['date', 'open', 'close', 'high', 'low', 'volume']\n",
    "        # 确保 'date' 列是 datetime 类型\n",
    "        etf_df['date'] = pd.to_datetime(etf_df['date'])\n",
    "        # 将date设为索引\n",
    "        etf_df.index = pd.to_datetime(etf_df['date'])\n",
    "        \n",
    "        self.data = etf_df\n",
    "        return self.data\n",
    "\n",
    "# 示例使用\n",
    "symbol = \"510050\"\n",
    "adjust = \"hfq\"\n",
    "\n",
    "etf = etf_daily(symbol,  adjust)\n",
    "data = etf.get_data()\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 基金分时数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                      时间   开盘     收盘     最高     最低     成交量         成交额      均价\n",
      "0    2024-06-25 09:30:00  0.0  0.812  0.812  0.812   76711   6228933.0  0.8120\n",
      "1    2024-06-25 09:31:00  0.0  0.811  0.814  0.811  202274  16433057.0  0.8123\n",
      "2    2024-06-25 09:32:00  0.0  0.813  0.813  0.811   65760   5339099.0  0.8122\n",
      "3    2024-06-25 09:33:00  0.0  0.813  0.814  0.813  115894   9422601.0  0.8124\n",
      "4    2024-06-25 09:34:00  0.0  0.811  0.813  0.811   89606   7277041.0  0.8124\n",
      "..                   ...  ...    ...    ...    ...     ...         ...     ...\n",
      "959  2024-06-28 14:56:00  0.0  0.783  0.784  0.783  202086  15826823.0  0.7945\n",
      "960  2024-06-28 14:57:00  0.0  0.783  0.784  0.782  113551   8891308.0  0.7945\n",
      "961  2024-06-28 14:58:00  0.0  0.784  0.784  0.783   79888   6260831.0  0.7944\n",
      "962  2024-06-28 14:59:00  0.0  0.784  0.784  0.783   87959   6891591.0  0.7944\n",
      "963  2024-06-28 15:00:00  0.0  0.783  0.784  0.783  128894  10096407.0  0.7943\n",
      "\n",
      "[964 rows x 8 columns]\n"
     ]
    }
   ],
   "source": [
    "import akshare as ak\n",
    "from datetime import datetime\n",
    "# 获取证券ETF的历史数据\n",
    "symbol = \"512880\"  # 例如：上证50ETF的代码\n",
    "start_date = datetime(2024,6,1)\n",
    "end_date = datetime(2024,7,1)\n",
    "\n",
    "\n",
    "fund_etf_hist_min_em_df = ak.fund_etf_hist_min_em(symbol=symbol, \n",
    "                                                  period=\"1\", adjust=\"\", \n",
    "                                                #   start_date=\"2024-06-20 09:30:00\", \n",
    "                                                #   end_date=\"2024-07-01 17:40:00\"\n",
    "                                                  start_date=start_date, \n",
    "                                                  end_date=end_date)\n",
    "print(fund_etf_hist_min_em_df)\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
